﻿@{
    User user = ViewBag.user;
    UserInfo info = ViewBag.info;
    ViewBag.Title = "个人设置";
    Layout = "~/Views/Shared/_layout.cshtml";
}
@section sidebar {
    <li><a href="/User/Edit/@user.uid">编辑资料</a></li>
    @Html.Partial("_sidebar")
}
<div class="es-form">
    <form id="resetemail" action="/User/ChangeEmail">
        <div class="row"><h3>修改邮箱</h3></div>
        <div class="row">
            <div class="col-md-2">电子邮箱</div>
            <div class="col-md-10">
                <input class="form-control" type="email" name="email" value="@info.email" required />
            </div>
        </div>
        <div class="row">
            <div class="col-md-2">
                <input class="btn btn-primary btn-sm" type="submit" value="提交" />
            </div>
        </div>
    </form>
    <hr />
    <form id="resetpassword" action="/User/ChangePassword">
        <div class="row"><h3>修改密码</h3></div>
        <div class="row">
            <div class="col-md-2">旧密码</div>
            <div class="col-md-10">
                <input class="form-control" type="password" id="oldpassword" name="oldpassword" data-toggle="popover" data-placement="bottom" data-content="密码错误" required />
            </div>
        </div>
        <div class="row">
            <div class="col-md-2">新密码</div>
            <div class="col-md-10">
                <input class="form-control" type="password" name="password" id="password" required />
            </div>
        </div>
        <div class="row">
            <div class="col-md-2">确认密码</div>
            <div class="col-md-10">
                <input class="form-control" type="password" id="confirm" name="confirm" data-toggle="popover" data-placement="bottom" data-content="两次输入的密码不一致" required />
            </div>
        </div>
        <div class="row">
            <div class="col-md-2">
                <input class="btn btn-primary btn-sm disabled" id="submit" type="submit" value="提交" />
            </div>
        </div>
    </form>
</div>
<script>
    $('#resetemail').submit(function (event) {
        $.ajax({
            type: "POST",
            url: "/User/ChangeEmail",
            data: $(this).serialize(),
            success: function (data) {
                if (data === true) {
                    alert("修改成功");
                    $('#name').val("")
                } else {
                    alert("修改失败");
                }
            }
        });
        event.preventDefault();
    })

    $('#oldpassword').blur(function () {
        $.ajax({
            type: "POST",
            url: "/User/PasswordValidate",
            data: "password=" + $(this).val(),
            success: function (data) {
                if (data === true) {
                    $("#submit").removeClass("disabled");
                } else {
                    $('#oldpassword').popover('show');
                }
            }
        });
    })

    $('#confirm').blur(function () {
        if ($(this).val() != $('#password').val()) {
            $(this).popover('show');
        }
    })

    $('#resetpassword').submit(function (event) {
        $.ajax({
            type: "POST",
            url: "/User/ChangePassword",
            data: $(this).serialize(),
            success: function (data) {
                if (data === true) {
                    alert("修改成功，请重新登录");
                    logout();
                } else {
                    alert("修改失败");
                }
            }
        });
        event.preventDefault();
    })
</script>